{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9a97d63a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 购物车程序项目：\n",
    "\n",
    "# 要求：1、运行程序后，让(用户输入)->input 支付宝余额，然后(打印)->格式化打印（占位符，format）我们的商品（列表）给用户。\n",
    "\n",
    "# 　　   2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "# 　　   3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "# 　　   4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "915761f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\t\t\t欢迎光临商城！！！\n",
      "\n",
      "------------------<商品列表>------------------\n",
      "\t\t\t0、iphone6s   \t￥5800\n",
      "\t\t\t1、mac book   \t￥9000\n",
      "\t\t\t2、coffee     \t￥32\n",
      "\t\t\t3、python book\t￥80\n",
      "\t\t\t4、bicycle    \t￥1500\n",
      "----------------------------------------------\n",
      "请输入您的工资：\t>>>7800\n",
      "请选择您的预购商品或退出[0/1/2/3/4][q]：\t>>>1\n",
      "\n",
      "已加入  mac book   到您的购物车 \n"
     ]
    }
   ],
   "source": [
    "# Author:  zqy\n",
    "\n",
    "def main():\n",
    "\n",
    "    print(\"\\n\\t\\t\\t欢迎光临商城！！！\\n\")\n",
    "\n",
    "    go_shopping = True\t\t# 程序运行标志\n",
    "    buy_git_num = []\t\t# 存储用户选择商品编号\n",
    "\n",
    "    merchandise_info = [(\"iphone6s   \", 5800), (\"mac book   \", 9000), (\"coffee     \", 32), (\"python book\", 80), (\"bicycle    \", 1500)]\t\t# 商品信息\n",
    "\n",
    "    \"\"\"\t# 1、利用枚举遍历打印商品列表\n",
    "    for i, v in enumerate(merchandise_info, 1):\n",
    "         print(\"\\t\\t\\t%d、%s\\t\\t￥%d\"%(i, v[0], v[1]))\n",
    "    \"\"\"\n",
    "    # 2、利用for , 以及python内置的 .index() 方法取其下标 , 打印商品列表\n",
    "    str1 = \"<商品列表>\"\n",
    "    print(str1.center(42, \"-\"))\n",
    "    for name_into in merchandise_info:\n",
    "        print(\"\\t\\t\\t%d、%s\\t￥%d\"%(merchandise_info.index(name_into), name_into[0], name_into[1]))\n",
    "\n",
    "    print(\"-\"*46)\n",
    "    salary_input_flag = True\t\t# 用户工资输入标志\n",
    "    while salary_input_flag:\t# 当salary_input_flag的值为真, 即salary_input_flag = True时, 进行循环\n",
    "        salary = input(\"请输入您的工资：\\t>>>\")\t# 接受用户的输入\n",
    "\n",
    "        if salary.isdigit():\t# 通过 .isdigit() 方法 判断输入的字符是否为数字\n",
    "           salary = int(salary)\t\t# 是“数字”, 则将其转为整型\n",
    "           salary_input_flag = False\t# 将工资输入标志设为假, 即salary_input_flag = False , 结束该层循环\n",
    "\n",
    "        else:\t# 输入的工资不是“数字”, 重新输入\n",
    "            salary_input_flag = True\t\t# 将工资输入标志设为真, 即salary_input_flag = True , 继续该层循环\n",
    "            print(\"\\n工资输入错误！！！\\n\")\n",
    "\n",
    "    while go_shopping:\t\t# 当go_shopping 的值为 True 时 , 开始该循环\n",
    "        buy_input_flag = True\t# 用户购买商品标志\n",
    "\n",
    "        while buy_input_flag:\t# 当 buy_input_flag 的值为 True ,开始购买循环\n",
    "            Quit = False\t# 初始化退出标志 为 False\n",
    "            choice = input(\"请选择您的预购商品或退出[0/1/2/3/4][q]：\\t>>>\")\n",
    "\n",
    "            if choice.isdigit():\t# 判断用户输入的商品号是否为“数字”\n",
    "                choice = int(choice)\t# 是“数字”, 则将其转为整型\n",
    "\n",
    "                if choice >= 0 and choice <= len(merchandise_info):\t\t# 判断用户输入的商品号是否存在\n",
    "                    buy_git_num.append(choice)\t# 存在, 则将其添加到加入购物车的列表中\n",
    "                    buy_input_flag = False\t\t# 同时, 将购买商品标志设为False, 结束该循环\n",
    "\n",
    "                else:\t# 若商品不存在, 给用户提示\n",
    "                    buy_input_flag = True\n",
    "                    print(\"\\n商品不存在！！！\")\n",
    "                    continue\t# 此处后不再向下执行，而是开始新的循环\n",
    "\n",
    "            elif choice == \"q\" or choice == \"Q\": \t# 判断用户输入的字符是否，不是“数字”, 并且 为“q”或者“Q”\n",
    "                Quit = True\t\t# 将退出标志设为True\n",
    "                go_shopping = False\t\t# 将购物标志设为False , 即该次循环结束后，不在继续循环\n",
    "\n",
    "            else:\t# 当用户输入的字符 不是【“数字”、“Q”、“q”】之一时\n",
    "                buy_input_flag = True\t# 设购买标志为True （此处可不设，因为其值本身初始化为True，并且没有改变）\n",
    "                print(\"\\n商品不存在！！！\")\n",
    "                continue\t# 此处后不再向下执行，而是开始新的循环\n",
    "\n",
    "            if Quit:\t# 如果退出标志被设置或改为True ，执行if下的子代码块\n",
    "                input_quit = input(\"是否支付购物车内的商品？[Y/N]\\n>>>\")\t# 接受用户命令\n",
    "\n",
    "                if input_quit == \"Y\" or input_quit == \"y\":\t# 接受到相应的命令\n",
    "                    str2 = \"<您已购买一下商品>\"\n",
    "                    print(\"\\n\", str2.center(40, \"-\"), end=\"\")\n",
    "\n",
    "                    for i in range(len(buy_git_num)):\t# 循环遍历，打印用户选择的商品信息\n",
    "                        print(\"\\n\\t{}、{}\\t￥{}\".format(i + 1, merchandise_info[i][0], merchandise_info[i][1]))\n",
    "                        salary -= int(merchandise_info[i][1])\t# 用户余额\n",
    "                        print(\"\\t剩余：%d\"%(salary))\n",
    "\n",
    "                        if salary <= 0:\t\t# 资不抵债, 直接退出\n",
    "                            exit(\"剩余：  ￥%d\\n余额不足！！！\" % (salary,))\n",
    "\n",
    "                else:\t# 接受到的字符为非“Y”、“y”\n",
    "                    str3 = \"<您购物车中的商品>\"\n",
    "                    print(\"\\n\", str3.center(42, \"-\"), end=\"\")\n",
    "                    for i in range(len(buy_git_num)):\t\t# 打印用户选择的商品（购物车）信息\n",
    "                        print(\"\\n\\t{}、{}\\t￥{}\".format(i + 1, merchandise_info[i][0], merchandise_info[i][1]))\n",
    "\n",
    "                print(\"\\n\\t您当前的余额为：  ￥%d\"%(salary,))\n",
    "                print(\"-\"*46)\n",
    "                print(\"\\n\\t\\t\\t\\t欢迎下次光临!!!\")\n",
    "                go_shopping = False\t\t# 同时将购物标志设为False，用于结束外层循环\n",
    "                break\t# 从此出跳出（结束）该层循环\n",
    "\n",
    "            print(\"\\n已加入  {}到您的购物车 \".format(merchandise_info[choice][0]))\n",
    "\n",
    "if __name__ == '__main__':  # 主程序入口\n",
    "    main()\t\t# 调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b942f32",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
